package com.hcw.blog.base.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.hcw.blog.base.enumdata.ExceptionCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResultVo<T> implements Serializable {
    private static final long serialVersionUID = 6803556997799085309L;

    /**
     * 返回值代码
     */
    private String code;

    /**
     * 返回数据体
     */
    private T data;

    /**
     * 消息描述
     */
    private String msg;

    ApiResultVo(ExceptionCodeEnum exceptionCodeEnum) {
        this.code = exceptionCodeEnum.getCode();
        this.msg = exceptionCodeEnum.getMsg();
    }


    public static <T> ApiResultVo<T> success() {
        return new ApiResultVo<T>(ExceptionCodeEnum.SUCCESS);
    }

    public static <T> ApiResultVo<T> success(ExceptionCodeEnum apiCodeEnum, T data) {
        ApiResultVo<T> resultVo = new ApiResultVo<T>(apiCodeEnum);
        resultVo.setData(data);
        return resultVo;
    }

    public static <T> ApiResultVo<T> success(T data) {
        ApiResultVo<T> resultVo = success();
        resultVo.data = data;
        return resultVo;
    }

    public static <T> ApiResultVo<T> fail() {
        return new ApiResultVo<T>(ExceptionCodeEnum.FAIL);
    }


    public static <T> ApiResultVo<T> fail(ExceptionCodeEnum apiCodeEnum) {
        return new ApiResultVo<T>(apiCodeEnum);
    }


    public static <T> ApiResultVo<T> fail(String errorMsg) {
        ApiResultVo<T> resultVo = fail();
        resultVo.msg = errorMsg;
        return resultVo;
    }


    public static <T> ApiResultVo<T> weChatVo(boolean isSuccess) {
        ApiResultVo<T> resultVo = new ApiResultVo<>();
        resultVo.setCode(isSuccess ? "SUCCESS" : "FAIL");
        resultVo.setMsg(isSuccess ? "成功" : "失败");
        return resultVo;
    }
}
